﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;

namespace TS_XY_Train_Repaire.Main_Base.DigitalMap
{
    public partial class Main_Base_DigitalMap_Element_ManaForm : DevExpress.XtraEditors.XtraForm
    {
        public Main_Base_DigitalMap_Element_ManaForm()
        {
            InitializeComponent();
        }

        private void Main_Base_DigitalMap_Element_ManaForm_Load(object sender, EventArgs e)
        {
            InitData();
        }

        private void InitData()
        {
            InitElementData();
        }

        private void InitElementData()
        {
            DataTable dt = new DataTable();
            TS_Win_Class.FilledDataTable("select _Element.f_id,_Element.f_name,_Element.f_size,case _Element.f_type when '0' then '工位' when '1' then '左下角插入元素' when '2' then '右下角插入元素' when '3' then '线条' when '4' then '文字' ELSE '' END as f_type,_stuff.f_name as f_workerid,_Element.f_time from TS_S_B_Digital_Map_Element as _Element left join TS_S_T_P_Stuff as _stuff on _Element.f_workerid=_stuff.f_id where _Element.f_del=0", dt);
            gridControl_Element.DataSource = dt;
        }

        private void btn_liulan_Click(object sender, EventArgs e)
        {
            openFileDialog_UpLoad.Filter = "图片文件|*.jpg |图片文件|*.png";
            if (openFileDialog_UpLoad.ShowDialog() == DialogResult.OK)
            {
                if (openFileDialog_UpLoad.FileName != "")
                {
                    pcbx_UppImg.ImageLocation = openFileDialog_UpLoad.FileName;
                }
            }
        }

        private void Main_Base_DigitalMap_Element_ManaForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            TS_Win_Locomotive_MainForm.htActionItem.Remove(this.Name);
        }


        private void btn_Add_Click(object sender, EventArgs e)
        {
            try
            {
                string sql = "";
                if (cmbx_Type.Text == "工位")
                {
                    if (pcbx_UppImg.ImageLocation != "")
                    {
                        FileStream fs = new FileStream(pcbx_UppImg.ImageLocation, FileMode.Open, FileAccess.Read);
                        System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
                        byte[] bt = br.ReadBytes(Convert.ToInt32(fs.Length));
                        string base64String = Convert.ToBase64String(bt);
                        br.Close();
                        fs.Close();

                        Bitmap bmp = new Bitmap(openFileDialog_UpLoad.FileName);
                        sql = "insert into TS_S_B_Digital_Map_Element (f_name,f_brief,f_size,f_type,f_workerid,f_time) values('" + txtf_name.Text.Trim() + "','" + base64String + "','" + bmp.Width.ToString() + "," + bmp.Height.ToString() + "','" + 0 + "'," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",getdate())";
                    }
                    else
                    {
                        MessageBox.Show("请选择图片！");
                    }
                }
                if (cmbx_Type.Text == "左下角插入元素")
                {
                    if (pcbx_UppImg.ImageLocation != "")
                    {
                        FileStream fs = new FileStream(pcbx_UppImg.ImageLocation, FileMode.Open, FileAccess.Read);
                        System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
                        byte[] bt = br.ReadBytes(Convert.ToInt32(fs.Length));
                        string base64String = Convert.ToBase64String(bt);
                        br.Close();
                        fs.Close();

                        Bitmap bmp = new Bitmap(openFileDialog_UpLoad.FileName);
                        sql = "insert into TS_S_B_Digital_Map_Element (f_name,f_brief,f_size,f_type,f_workerid,f_time) values('" + txtf_name.Text.Trim() + "','" + base64String + "','" + bmp.Width.ToString() + "," + bmp.Height.ToString() + "','" + 1 + "'," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",getdate())";
                    }
                    else
                    {
                        MessageBox.Show("请选择图片！");
                    }
                }
                if (cmbx_Type.Text == "右下角插入元素")
                {
                    if (pcbx_UppImg.ImageLocation != "")
                    {
                        FileStream fs = new FileStream(pcbx_UppImg.ImageLocation, FileMode.Open, FileAccess.Read);
                        System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
                        byte[] bt = br.ReadBytes(Convert.ToInt32(fs.Length));
                        string base64String = Convert.ToBase64String(bt);
                        br.Close();
                        fs.Close();

                        Bitmap bmp = new Bitmap(openFileDialog_UpLoad.FileName);
                        sql = "insert into TS_S_B_Digital_Map_Element (f_name,f_brief,f_size,f_type,f_workerid,f_time) values('" + txtf_name.Text.Trim() + "','" + base64String + "','" + bmp.Width.ToString() + "," + bmp.Height.ToString() + "','" + 2 + "'," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",getdate())";
                    }
                    else
                    {
                        MessageBox.Show("请选择图片！");
                    }
                }
                if (cmbx_Type.Text == "线条")
                {
                    sql = "insert into TS_S_B_Digital_Map_Element (f_name,f_type,f_workerid,f_time) values('" + txtf_name.Text.Trim() + "'," + 3 + "," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",getdate())";
                }
                if (cmbx_Type.Text == "文字")
                {
                    sql = "insert into TS_S_B_Digital_Map_Element (f_name,f_type,f_size,f_workerid,f_time) values('" + txtf_name.Text.Trim() + "'," + 4 + ",'" + txtf_size.Text.Trim() + "'," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",getdate())";
                }
                if (cmbx_Type.Text == "摄像头")
                {
                    if (pcbx_UppImg.ImageLocation != "")
                    {
                        FileStream fs = new FileStream(pcbx_UppImg.ImageLocation, FileMode.Open, FileAccess.Read);
                        System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
                        byte[] bt = br.ReadBytes(Convert.ToInt32(fs.Length));
                        string base64String = Convert.ToBase64String(bt);
                        br.Close();
                        fs.Close();

                        Bitmap bmp = new Bitmap(openFileDialog_UpLoad.FileName);
                        sql = "insert into TS_S_B_Digital_Map_Element (f_name,f_brief,f_size,f_type,f_workerid,f_time) values('" + txtf_name.Text.Trim() + "','" + base64String + "','" + bmp.Width.ToString() + "," + bmp.Height.ToString() + "','" + 5 + "'," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",getdate())";
                    }
                    else
                    {
                        MessageBox.Show("请选择图片！");
                    }
                }
                TS_Win_Class.MySqlCom(sql);
                txtf_name.Text = "";
                txtf_size.Text = "";
                cmbx_Type.Text = "";
                pcbx_UppImg.Image = null;
                pcbx_ImgInfo.Image = null;
                InitElementData();
            }
            catch
            {

            }
        }

        private void cmbx_Type_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbx_Type.Text == "工位")
            {
                btn_liulan.Enabled = true;
                txtf_size.Enabled = false;
            }
            if (cmbx_Type.Text == "左下角插入元素")
            {
                btn_liulan.Enabled = true;
                txtf_size.Enabled = false;
            }
            if (cmbx_Type.Text == "右下角插入元素")
            {
                btn_liulan.Enabled = true;
                txtf_size.Enabled = false;
            }
            if (cmbx_Type.Text == "线条")
            {
                btn_liulan.Enabled = false;
                txtf_size.Enabled = false;
            }
            if (cmbx_Type.Text == "文字")
            {
                btn_liulan.Enabled = false;
                txtf_size.Enabled = true;
            }
            if (cmbx_Type.Text == "摄像头")
            {
                btn_liulan.Enabled = true;
                txtf_size.Enabled = false;
            }
        }

        private void btn_Edit_Click(object sender, EventArgs e)
        {
            string _id = gridView1.GetFocusedRowCellValue(gridView1.Columns["f_id"]).ToString();
            string sql = "";
            try
            {
                if (cmbx_Type.Text == "工位")
                {
                    if (pcbx_UppImg.ImageLocation != "")
                    {
                        FileStream fs = new FileStream(openFileDialog_UpLoad.FileName, FileMode.Open, FileAccess.Read);
                        System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
                        byte[] bt = br.ReadBytes(Convert.ToInt32(fs.Length));
                        string base64String = Convert.ToBase64String(bt);
                        br.Close();
                        fs.Close();

                        Bitmap bmp = new Bitmap(openFileDialog_UpLoad.FileName);
                        if (TS_Win_Class.GetFilledDataTable("select * from TS_S_B_Digital_Map_Element where f_del=0 and f_id=" + _id + "").Rows.Count == 0)
                        {
                            sql = "insert into TS_S_B_Digital_Map_Element (f_name,f_brief,f_size,f_type,f_workerid,f_time) values('" + txtf_name.Text.Trim() + "','" + base64String + "','" + bmp.Width.ToString() + "," + bmp.Height.ToString() + "'," + 0 + "," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",getdate())";
                        }
                        else
                        {
                            sql = "update TS_S_B_Digital_Map_Element set f_name='" + txtf_name.Text.Trim() + "',f_brief='" + base64String + "',f_size='" + bmp.Width.ToString() + "," + bmp.Height.ToString() + "',f_type=" + 0 + ",f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_id=" + _id;
                        }
                    }
                    else
                    {
                        MessageBox.Show("请选择图片！");
                    }
                }
                if (cmbx_Type.Text == "左下角插入元素")
                {
                    if (pcbx_UppImg.ImageLocation != "")
                    {
                        FileStream fs = new FileStream(openFileDialog_UpLoad.FileName, FileMode.Open, FileAccess.Read);
                        System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
                        byte[] bt = br.ReadBytes(Convert.ToInt32(fs.Length));
                        string base64String = Convert.ToBase64String(bt);
                        br.Close();
                        fs.Close();

                        Bitmap bmp = new Bitmap(openFileDialog_UpLoad.FileName);
                        if (TS_Win_Class.GetFilledDataTable("select * from TS_S_B_Digital_Map_Element where f_del=0 and f_id=" + _id + "").Rows.Count == 0)
                        {
                            sql = "insert into TS_S_B_Digital_Map_Element (f_name,f_brief,f_size,f_type,f_workerid,f_time) values('" + txtf_name.Text.Trim() + "','" + base64String + "','" + bmp.Width.ToString() + "," + bmp.Height.ToString() + "'," + 1 + "," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",getdate())";
                        }
                        else
                        {
                            sql = "update TS_S_B_Digital_Map_Element set f_name='" + txtf_name.Text.Trim() + "',f_brief='" + base64String + "',f_size='" + bmp.Width.ToString() + "," + bmp.Height.ToString() + "',f_type=" + 1 + ",f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_id=" + _id;
                        }
                    }
                    else
                    {
                        MessageBox.Show("请选择图片！");
                    }
                }
                if (cmbx_Type.Text == "右下角插入元素")
                {
                    if (pcbx_UppImg.ImageLocation != "")
                    {
                        FileStream fs = new FileStream(openFileDialog_UpLoad.FileName, FileMode.Open, FileAccess.Read);
                        System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
                        byte[] bt = br.ReadBytes(Convert.ToInt32(fs.Length));
                        string base64String = Convert.ToBase64String(bt);
                        br.Close();
                        fs.Close();

                        Bitmap bmp = new Bitmap(openFileDialog_UpLoad.FileName);
                        if (TS_Win_Class.GetFilledDataTable("select * from TS_S_B_Digital_Map_Element where f_del=0 and f_id=" + _id + "").Rows.Count == 0)
                        {
                            sql = "insert into TS_S_B_Digital_Map_Element (f_name,f_brief,f_size,f_type,f_workerid,f_time) values('" + txtf_name.Text.Trim() + "','" + base64String + "','" + bmp.Width.ToString() + "," + bmp.Height.ToString() + "'," + 2 + "," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",getdate())";
                        }
                        else
                        {
                            sql = "update TS_S_B_Digital_Map_Element set f_name='" + txtf_name.Text.Trim() + "',f_brief='" + base64String + "',f_size='" + bmp.Width.ToString() + "," + bmp.Height.ToString() + "',f_type=" + 2 + ",f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_id=" + _id;
                        }
                    }
                    else
                    {
                        MessageBox.Show("请选择图片！");
                    }
                }
                if (cmbx_Type.Text == "线条")
                {
                    sql = "update TS_S_B_Digital_Map_Element set f_name='" + txtf_name.Text.Trim() + "',f_type=" + 3 + ",f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_id=" + _id;
                }
                if (cmbx_Type.Text == "文字")
                {
                    sql = "update TS_S_B_Digital_Map_Element set f_name='" + txtf_name.Text.Trim() + "',f_type=" + 3 + ",f_size='" + txtf_size.Text.Trim() + "',f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_id=" + _id;
                }
                if (cmbx_Type.Text == "摄像头")
                {
                    if (pcbx_UppImg.ImageLocation != "")
                    {
                        FileStream fs = new FileStream(openFileDialog_UpLoad.FileName, FileMode.Open, FileAccess.Read);
                        System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
                        byte[] bt = br.ReadBytes(Convert.ToInt32(fs.Length));
                        string base64String = Convert.ToBase64String(bt);
                        br.Close();
                        fs.Close();

                        Bitmap bmp = new Bitmap(openFileDialog_UpLoad.FileName);
                        if (TS_Win_Class.GetFilledDataTable("select * from TS_S_B_Digital_Map_Element where f_del=0 and f_id=" + _id + "").Rows.Count == 0)
                        {
                            sql = "insert into TS_S_B_Digital_Map_Element (f_name,f_brief,f_size,f_type,f_workerid,f_time) values('" + txtf_name.Text.Trim() + "','" + base64String + "','" + bmp.Width.ToString() + "," + bmp.Height.ToString() + "'," + 5 + "," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",getdate())";
                        }
                        else
                        {
                            sql = "update TS_S_B_Digital_Map_Element set f_name='" + txtf_name.Text.Trim() + "',f_brief='" + base64String + "',f_size='" + bmp.Width.ToString() + "," + bmp.Height.ToString() + "',f_type=" + 5 + ",f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_id=" + _id;
                        }
                    }
                    else
                    {
                        MessageBox.Show("请选择图片！");
                    }
                }
                TS_Win_Class.MySqlCom(sql);
                txtf_name.Text = "";
                txtf_size.Text = "";
                cmbx_Type.Text = "";
                pcbx_UppImg.Image = null;
                pcbx_ImgInfo.Image = null;
                InitElementData();
            }
            catch
            {

            }



        }

        private void btn_Del_Click(object sender, EventArgs e)
        {
            string _id = gridView1.GetFocusedRowCellValue(gridView1.Columns["f_id"]).ToString();
            string sql = "update TS_S_B_Digital_Map_Element set f_del=1,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_id=" + _id;
            TS_Win_Class.MySqlCom(sql);
            txtf_name.Text = "";
            txtf_size.Text = "";
            cmbx_Type.Text = "";
            InitElementData();
        }

        private void gridControl_Element_Click(object sender, EventArgs e)
        {
            try
            {
                pcbx_ImgInfo.Image = null;
                string _id = gridView1.GetFocusedRowCellValue(gridView1.Columns["f_id"]).ToString();
                txtf_name.Text = gridView1.GetFocusedRowCellValue(gridView1.Columns["f_name"]).ToString();
                txtf_size.Text = gridView1.GetFocusedRowCellValue(gridView1.Columns["f_size"]).ToString();
                cmbx_Type.Text = gridView1.GetFocusedRowCellValue(gridView1.Columns["f_type"]).ToString();
                DataTable dt = TS_Win_Class.GetFilledDataTable("select f_brief from TS_S_B_Digital_Map_Element where f_del=0 and f_id=" + _id + "");
                if (dt.Rows.Count > 0)
                {
                    MemoryStream ms = new MemoryStream(Convert.FromBase64String(dt.Rows[0]["f_brief"].ToString()));
                    Image image = Image.FromStream(ms, true);
                    pcbx_ImgInfo.Image = image;
                }
            }
            catch
            {

            }
        }
    }
}